const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    defaultViewport: {
      width: 1920,
      height: 1080
    }
  });
  const page = await browser.newPage();
  await page.goto('https://i.xunlei.com/xluser/login.html');

  // setTimeout(async()=>{
    // const $iu = await page.$('#loginIframe');

    // page.waitForSelector('#loginIframe').then(async($frame) => {
    //   const $i = await $frame.$('#input');
    //   console.log($i);
    //   await page.screenshot({ path: 'xl.png' });
    //   await browser.close();
    // });

    page.frames().map(async(frame) => {
      if(frame.url().indexOf('https://i.xunlei.com/login') === 0) {
        const $u = await frame.waitForSelector('#al_u');
        const $p = await frame.waitForSelector('#al_p');
        const $submit = await frame.waitForSelector('#al_submit');
        await $u.type('1702589961');
        await $p.type('123456abc');
        $submit.click();
        setTimeout(async()=>{
          // await page.goto('https://i.xunlei.com/xluser/account/acc_safe.html');
          await page.goto('https://i.xunlei.com/xluser/validate/modifypwd_p.html');
          // const $loginpwdModifyBtn = page.waitForSelector('#loginpwd-modify-btn')
          // $loginpwdModifyBtn.click();
          await page.screenshot({ path: 'xl.png' });
          await browser.close();
        }, 5000);
      }
    })

  // }, 3000);
})();